#include"../include/file_descriptor.h"
#include"../include/block.h"
#include"../include/main_header.h"

#include<stdio.h>
void create_blocks()
{

unsigned long int i,temp;

//goto starting location in VFS
rewind(fp);
struct block b;
//jump to size of (metaheader+one block(root dir block))
fseek(fp,sizeof(struct main_header)+BLOCK_SIZE,0);

while(ftell(fp)<m.vfs_size)
{
	
	//generate address of next block	
	b.next=ftell(fp)+BLOCK_SIZE;

	//write block  to the file	
	fwrite(&b,sizeof(struct block),1,fp);
}
fseek(fp,-(2*BLOCK_SIZE),1);
b.next=0xFFFF;

	//write block  to the file	
	fwrite(&b,sizeof(struct block),1,fp);
}

unsigned long int get_free_block()
{
	struct block b;
	unsigned long int temp;
//save the address of first free block in temp in which the file will be stored	
	temp=m.free_address;	

//jump to that block and get the address of next free block and update in meta header
	fseek(fp,m.free_address,0);
	fread(&b,sizeof(struct block),1,fp);
	m.free_address=b.next;
	b.next=0xFFFF;
	fseek(fp,-(sizeof(struct block)),1);
	fwrite(&b,sizeof(struct block),1,fp);
	update_main_header();
	return temp;


	
}
void get_addresses()
{
struct block b;
int i;

//start of VFS
rewind(fp);

//jump to first block following the main header
fseek(fp,sizeof(struct main_header),0);

for(i=0;i<m.max_descriptors;i++)
{

//read one block
fread(&b,sizeof(struct block),1,fp);

//print the address of the next block
//printf("%ld\n",b.next);
}
}
